﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AudioCore.Common
{
    public class DateUtils
    {
        /// <summary>
        /// Compare 2 time value follow format hh:mm:ss
        /// </summary>
        /// <param name="time1"></param>
        /// <param name="time2"></param>
        /// <returns></returns>
        public static int CompareTime(DateTime time1, DateTime time2)
        {
            DateTime temp1 = new DateTime(1970, 1, 1, time1.Hour, time1.Minute, time1.Second);
            DateTime temp2 = new DateTime(1970, 1, 1, time2.Hour, time2.Minute, time2.Second);
            return temp1.CompareTo(temp2);
        }

        /// <summary>
        /// Convert a string date format to a DateTime structure.
        /// <br>Allowed format date: d, d.m, d.m.yyyy</br>
        /// </summary>
        /// <param name="strDate"></param>
        /// <returns></returns>
        public static bool StringToDate(string strDate, ref DateTime date)
        {
            bool result = false;
            DateTime curDate = DateTime.Now;
            if (string.IsNullOrEmpty(strDate))
            {
                return false;
            }
            string[] dateParts = strDate.Trim().Split(Constant.DatePartSeparator.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            int numberOfPart = dateParts.Length;
            int day, month, year;
            try
            {
                switch (numberOfPart)
                {
                    case 1: //Only day of month
                        {
                            if (int.TryParse(dateParts[0], out day))
                            {
                                //Using current month & year
                                date = new DateTime(curDate.Year, curDate.Month, day);
                                result = true;
                            }
                            break;
                        }
                    case 2: //Only day & month
                        {
                            if ((int.TryParse(dateParts[0], out day)) && ((int.TryParse(dateParts[1], out month))))
                            {
                                //Using current month & year
                                date = new DateTime(curDate.Year, month, day);
                                result = true;
                            }
                            break;
                        }
                    case 3: //day, month, year
                        {
                            if ((int.TryParse(dateParts[0], out day)) && ((int.TryParse(dateParts[1], out month)))
                                && ((int.TryParse(dateParts[2], out year))) && (year > 999))
                            {
                                //Using current month & year
                                date = new DateTime(year, month, day);
                                result = true;
                            }
                            break;
                        }
                    default:
                        {
                            result = false;
                            break;
                        }
                }
            }
            catch (Exception ex)
            {
                //TODO: Write log or not?
                result = false;
            }
            return result;
        }

        public static string ToString(DateTime date)
        {
            string result = string.Empty;
            try
            {
                DateTime now = DateTime.Now;

                //Console.WriteLine(now.Ticks - date.Ticks);

                DateTime d = now.Add(new TimeSpan(-date.Ticks));

                if (d.Year > 1)
                    result = string.Format("{0} năm trước", d.Year);
                else if (d.Month > 1)
                    result = string.Format("{0} tháng trước", d.Month);
                else if (d.Day > 1)
                    result = string.Format("{0} ngày trước", d.Day);
                else if (d.Hour >= 1)
                    result = string.Format("{0} giờ trước", d.Hour);
                else if (d.Minute >= 1)
                    result = string.Format("{0} phút trước", d.Minute);
                else if (d.Second >= 1)
                    result = string.Format("{0} giây trước", d.Second);
               
                else
                    result = date.ToString("h:m d/M/yyyy");

                return result;
            }
            catch (Exception ex)
            {
                return string.Empty;
            }
        }
    }
}
